package com.forotuxpan.appforotuxpan;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Rect;
import android.os.AsyncTask;
import android.os.Build;
import android.os.StrictMode;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;


public class EmpresaDetalleActivity extends BaseActionBarActivity {

    Activity activity;
    String logotipo="";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detalle_empresa);

        activity = this;
        Bundle bundle = getIntent().getExtras();

        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }

        TextView txtNom = (TextView)findViewById(R.id.txtNomEmpresa);
        TextView txtTel = (TextView)findViewById(R.id.txtTelefono);
        TextView txtMail = (TextView)findViewById(R.id.txtEmail);
        TextView txtDir = (TextView)findViewById(R.id.txtDireccion);
        ImageView ivLogo = (ImageView)findViewById(R.id.ivLogo);
        txtNom.setText(bundle.getString("Nombre"));
        txtTel.setText(bundle.getString("Telefono"));
        txtMail.setText(bundle.getString("Email"));
        txtDir.setText(bundle.getString("Direccion"));
        logotipo = bundle.getString("Logotipo");
        getLogotipo();
    }

    private void getLogotipo() {
        task = new AsyncTask<Void, Void, Void>() {
            boolean isErrorConnec = false;
            @Override
            protected void onPreExecute() {
                showLoading(activity);
            }

            @Override
            protected Void doInBackground(Void... arg0) {
                try {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (isErrorConnec) {
                                Toast.makeText(getBaseContext(), getResources().getString(R.string.msg_problema_conexion), Toast.LENGTH_SHORT).show();
                            } else {
                                ImageView ivLogo = (ImageView)findViewById(R.id.ivLogo);
                                if (logotipo != "")
                                    ivLogo.setImageBitmap(getLogotipo(logotipo));
                                else
                                    ivLogo.setImageBitmap(getLogotipoDefault());
                            }
                        }
                    });
                }
                /*catch (InterruptedException e) {
                    //e.printStackTrace();
                    mCallbacks.hiddenCargando();
                }*/
                catch (Exception ex) {
                    hiddenLoading();
                }
                return null;
            }

            @Override
            protected void onPostExecute(Void result) {
                hiddenLoading();
            }

        };
        //task.execute((Void[])null);
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
            task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
        else
            task.execute((Void[]) null);
    }

    private Bitmap getLogotipo(String logotipo) {
        String pathImg = "http://forotuxpan.com/logotipos/" + logotipo;
        URL imageUrl = null;
        Bitmap img = null;
        HttpURLConnection conn = null;
        try {
            imageUrl = new URL(pathImg);
            conn = (HttpURLConnection) imageUrl.openConnection();
            conn.connect();

            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 2; // el factor de escala a minimizar la imagen, siempre es potencia de 2

            img = BitmapFactory.decodeStream(conn.getInputStream(), new Rect(0, 0, 0, 0), options);

        }
        catch (IOException e) {
            e.printStackTrace();

        }

        return img;
    }

    private Bitmap getLogotipoDefault() {
        String pathImg = "http://forotuxpan.com/images/logo_forotuxpan.gif";
        URL imageUrl = null;
        Bitmap img = null;
        HttpURLConnection conn = null;
        try {
            imageUrl = new URL(pathImg);
            conn = (HttpURLConnection) imageUrl.openConnection();
            conn.connect();

            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 2; // el factor de escala a minimizar la imagen, siempre es potencia de 2

            img = BitmapFactory.decodeStream(conn.getInputStream(), new Rect(0, 0, 0, 0), options);

        }
        catch (IOException e) {
            e.printStackTrace();

        }

        return img;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_detalle_empresa, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.home || id == 16908332 ) {
            this.finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
